

#ifndef _UTIL_3D_H_
#define _UTIL_3D_H_
#include "xtypes.h"

#include "xmath.h"
#include "util_matrix.h"
#include "util_image.h"
#include "xarray.h"

typedef TxArray2<float_t> zbuffer_t;

typedef struct {
    float_t left;
    float_t top;
    float_t right;
    float_t bottom;
}window_t;

typedef struct {
    int_t left;
    int_t top;
    int_t right;
    int_t bottom;
}viewport_t;

typedef struct {
	point3_t eye;
	point3_t center;
	vector3_t up;
}observeparam_t;


typedef struct {
	float_t fovy;
	float_t aspect;
	float_t znear;
	float_t zfar;
}frustum_t;

typedef _rgba_t color4_t;
typedef _rgb_t color3_t;

typedef struct {
	color4_t ambient;
	color4_t diffuse;
	color4_t specular;
	float_t shiness;
	color4_t emission;
	float_t opacity;
}material_t;



typedef struct {
	int_t nVertexAmt;
	TxArray2<point3_t> VerticsCrood;
	TxArray2<vector3_t> VerticsNormals;
	TxArray2<int_t> VerticsMaterialIndex;
	TxArray2<material_t> Materials;
	TxArray2<color4_t> VerticsColors;	
	TxArray2<_uv_t> TexCroods;
	lpstr_t szTexFile;
	TxArray2<int_t> EdgeStream;
}t_object;


typedef struct {
	uint_t		type;
	point3_t	pos;
	vector3_t	dir;
	uint_t		range;
	color4_t	color;
}light_t;

typedef struct{
	float_t A;
	float_t B;
	float_t C;
	float_t D;
}plane3equ_t;

void plane3_equation_calc(plane3equ_t & p3equ, point3_t p0, point3_t p1, point3_t p2);
float_t plane3_z_interpolation(plane3equ_t p3equ, float_t x, float_t y);

observeparam_t fnGenObserveCSParam(float_t xeye, float_t yeye, float_t zeye, float_t xcenter, float_t ycenter, float_t zcenter, float_t xup, float_t yup, float_t zup );
/*viewparam_t fnViewCSParam(float_t xeye, float_t yeye, float_t zeye, float_t xcenter, float_t ycenter, float_t zcenter, float_t pangle);*/
frustum_t fnGen3dFrustum(float_t fovy,float_t aspect,float_t znear, float_t zfar);
window_t fnGen2dWindow(float_t left, float_t top, float_t right, float_t bottom);
viewport_t fnGen2dViewport(int_t left, int_t top, int_t right, int_t bottom);
color4_t fnGenRGBA(float_t r, float_t g, float_t b, float_t a);

/*CMatrix4x4 fnObservationMatrix(viewparam_t view);*/
CMatrix4x4 fnObservationMatrix(observeparam_t observe);
CMatrix4x4 fnPerspectiveMatrix(observeparam_t observe);


CMatrix4x4 fnNormalizeMatrix(window_t window);
CMatrix4x4 fnRasterizationMatrix(viewport_t viewport);

color4_t fnCalcColorByLIM(point3_t target_pos, vector3_t target_normal, point3_t eye_pos, light_t light, material_t material, color4_t environment);
CMatrix4x4 fnTextureMap(rpoint_t p0, rpoint_t p1, rpoint_t p2, point2_t texc0, point2_t texc1, point2_t texc2);



//slfr_t: type for scanline-fill result
typedef struct {
	int_t sl_amount;
	int_t sl_start;
	TxArray<int_t> sl_se;
}slfr_t;
void triangle_fill_calc(slfr_t & slfr, rpoint_t p0, rpoint_t p1, rpoint_t p2);

void DoGouraudShadingForTriangle(TxArray<color4_t> & cr_se, point3_t p3d0, point3_t p3d1, point3_t p3d2, color4_t cr0, color4_t cr1, color4_t cr2);

void draw_triangle_blanking_flat(CxSphereCanvas& canvas, zbuffer_t& zbuf, point3_t p3d0, point3_t p3d1, point3_t p3d2, color4_t cr);
void draw_triangle_blanking_smooth(CxSphereCanvas& canvas, zbuffer_t& zbuf, point3_t p3d0, point3_t p3d1, point3_t p3d2, color4_t cr0, color4_t cr1, color4_t cr2);
void draw_triangle_texture_nolight(CxSphereCanvas& canvas, zbuffer_t& zbuf, point3_t p3d0, point3_t p3d1, point3_t p3d2, CxSphereCanvas& texture, _uv_t texc0, _uv_t texc1, _uv_t texc2);
void draw_triangle_smooth_texture(CxSphereCanvas& canvas, zbuffer_t& zbuf, point3_t p3d0, point3_t p3d1, point3_t p3d2, color4_t cr0, color4_t cr1, color4_t cr2, CxSphereCanvas& texture, _uv_t texc0, _uv_t texc1, _uv_t texc2);






#endif //_UTIL_3D_H_
